package com.zhongshengnetwork.rightbe.wzt.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhongshengnetwork.rightbe.Adv.model.AdvertUtils;
import com.zhongshengnetwork.rightbe.Adv.model.SPManageUtils;
import com.zhongshengnetwork.rightbe.Constant.Constant;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.AppThemeUtils;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.GsonTools;
import com.zhongshengnetwork.rightbe.common.TopBarView;
import com.zhongshengnetwork.rightbe.common.activity.AppBaseActivity;
import com.zhongshengnetwork.rightbe.common.customview.CustomDialog;
import com.zhongshengnetwork.rightbe.common.customview.CustomFlowLayout;
import com.zhongshengnetwork.rightbe.common.utils.ToastUtil;
import com.zhongshengnetwork.rightbe.gsonmodel.LangTypeModel;
import com.zhongshengnetwork.rightbe.gsonmodel.WeiShuLabelInfoModel;
import com.zhongshengnetwork.rightbe.lang.activity.SelectPublishTypeActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

public class WZTLabelActivity extends AppBaseActivity implements TopBarView.onTitleBarClickListener {

    private TopBarView topbar;
    private CustomFlowLayout flowLayout;
    private String[] mLabels = {"台词","影评","素材","写作","情感", "读书", "感悟", "英文", "方法", "技巧","经验","书评","日志","美景","美食"};
    private List<String> langNameList;
    private HashMap<String, String> langNameMap;
    private List<String> selectIds = null;
    private List<String> selectNames = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wztlabel);
        String labelIds = getIntent().getStringExtra("labelIds");
        selectIds = new ArrayList<String>();
        selectNames = new ArrayList<String>();
        if (!CommonUtils.isEmpty(labelIds)){
            List<String> ids = CommonUtils.stringToArrayList(labelIds);
            selectIds.addAll(ids);
        }
        topbar = (TopBarView)findViewById(R.id.topbar);
        topbar.getRightView().setVisibility(View.GONE);
        topbar.setClickListener(this);
        topbar.getRightTextView().setVisibility(View.VISIBLE);
        topbar.getRightTextView().setText("保存");
        topbar.getRightTextView().setTextSize(14);
        topbar.getRightTextView().setClickable(true);
        topbar.getRightTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectIds.size() == 0){
                    ToastUtil.show(WZTLabelActivity.this, "亲，请选择标签哦~");
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("labelName",CommonUtils.listToString(selectNames));
                intent.putExtra("labelId", CommonUtils.listToString(selectIds));
                setResult(RESULT_OK,intent);
                finish();
            }
        });

        LinearLayout wztlabel = (LinearLayout) findViewById(R.id.wztlabel);
        wztlabel.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
        TextView tip_text = (TextView) findViewById(R.id.tip_text);
        tip_text.setTextColor(AppThemeUtils.getInstance().getLightTextColor());

        initType();
    }

    private void initType(){
        flowLayout = (CustomFlowLayout) findViewById(R.id.mflowLayout);
        langNameList = new ArrayList<String>();
        langNameMap = new HashMap<String, String>();
        String data = SPManageUtils.getInstance(CustomApplication.mContext).getSPString(AdvertUtils.WZTUserLabelData,"");
        if (CommonUtils.isEmpty(data)){
            for (int i = 0; i < mLabels.length; i++){
                langNameList.add(mLabels[i]);
                langNameMap.put(mLabels[i],(i+1)+"");
                if (selectIds.size() > 0){
                    if (selectIds.contains((i+1)+"")){
                        if (selectNames.contains(mLabels[i]) == false){
                            selectNames.add(mLabels[i]);
                        }
                    }
                }
            }
        }else {
            List<WeiShuLabelInfoModel>l = GsonTools.getWeiShuLabelInfoModel(data);
            if (l != null && l.size() > 0){
                Log.e("TAG","网络请求的数据\n");
                for (int i = 0; i < l.size(); i++){
                    WeiShuLabelInfoModel m = l.get(i);
                    langNameList.add(m.getLabelName());
                    langNameMap.put(m.getLabelName(),m.getId()+"");
                    if (selectIds.size() > 0){
                        if (selectIds.contains(m.getId()+"")){
                            if (selectNames.contains(m.getLabelName()) == false){
                                selectNames.add(m.getLabelName());
                            }
                        }
                    }
                }
            }
        }
        flowLayout.setColorful(false);
        String[] strArrayTrue = (String[]) langNameList.toArray(new String[0]);
        flowLayout.setData(strArrayTrue, selectNames);
        flowLayout.setOnTagClickListener(new CustomFlowLayout.OnTagClickListener() {
            @Override
            public void TagClick(View v) {
                final TextView tv = (TextView)v;
                String title = tv.getText().toString();
                final int tag = langNameList.indexOf(title);
                String id = langNameMap.get(title);
                if (!CommonUtils.isEmpty(id)){
                     if (selectIds.contains(id) == true){
                         selectIds.remove(id);
                         selectNames.remove(title);
                         tv.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
                         tv.setBackgroundColor(AppThemeUtils.getInstance().getBackgroundColor());
                     }else {
                         if (selectIds.size() >= 3){
                             ToastUtil.show(WZTLabelActivity.this, "亲，最多可选择3个标签哦");
                             return;
                         }
                         selectIds.add(id);
                         selectNames.add(title);
                         tv.setTextColor(Color.parseColor("#99cc00"));
                         tv.setBackgroundResource(AppThemeUtils.getInstance().getWZTLabelSelectBackground());
                     }
                }

            }
        });
    }

    /**
     * 点击了返回导航按钮
     */
    @Override
    public void onBackClick() {
        finish();
    }

    /**
     * 点击了右边导航按钮
     */
    @Override
    public void onRightClick() {

    }

}
